package huhx.linux.com.kotlinlearn.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

import huhx.linux.com.kotlinlearn.base.BaseActivity;

public enum ActivityManager {

    INSTANCE;
    private static ArrayList<Activity> activities;

    public static ActivityManager getInstance() {
        if (activities == null || activities.size() < 1)
            activities = new ArrayList<Activity>();
        return INSTANCE;
    }

    public List<Activity> getActivitys() {
        return activities;
    }

    /**
     * 溢出堆栈的前一个acitivity
     *
     * @return
     */
    public Activity popActivity() {
        if (activities == null)
            return null;

        if (activities.size() == 0) {
            return null;
        }
        Activity activity = activities.get(activities.size() - 1);
        activities.remove(activity);
        activity.finish();
        return activity;
    }

    /**
     * 向堆栈中添加activity
     *
     * @param activity
     */
    public void pushActivity(Activity activity) {
        if (activities == null) {
            activities = new ArrayList<>();
        }

        if (activity instanceof BaseActivity) {

        }

        if (!activities.contains(activity.getClass())) {
            activities.add(activity);
        }
    }

    /**
     * 清空activity堆栈
     */
    public void clearActityStack() {
        if (activities != null) {
            for (Activity activity : activities) {
                if (activity != null)
                    activity.finish();
            }
            activities.clear();
            activities = null;
        }
    }

    /**
     * 清空activity堆栈
     */
    public void clearActityStackNotFinish() {
        activities = null;
    }

    public int getSize() {
        return activities.size();
    }

}
